package com.begye.smartloan.apptest.chouti

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.ChouTiDrawerBinding
import com.begye.smartloan.apptest.fangkui.FangKuiActivity
import com.begye.smartloan.apptest.renzheng.alldone.RenZhengMyProfileActivity
import com.begye.smartloan.apptest.sezhi.SeZhiActivity
import com.begye.smartloan.apptest.sezhi.SeZhiTouXiangActivity
import com.begye.smartloan.apptest.tool.SpTool
import com.begye.smartloan.apptest.tool.addTouch
import com.begye.smartloan.apptest.xieyi.XieYiActivity
import com.util.utillib.extend.IntentFlagMode
import com.util.utillib.extend.toNextActivity

class ChouTiDrawer : LinearLayout {

    private var _binding: ChouTiDrawerBinding? = null
    private val binding get() = _binding!!

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        context?.let {
            val layout = View.inflate(it, R.layout.chou_ti_drawer, this)
            _binding = ChouTiDrawerBinding.bind(layout)
        }
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        // Ensure binding is not null after inflation
        if (_binding == null) {
            throw IllegalStateException("ViewBinding not initialized for ${javaClass.simpleName}")
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        _binding = null  // Clear the binding reference
    }

    fun refresh(parentActivity: AppCompatActivity? = null, onClickAction: (Int) -> Unit) {
        var local = SpTool.get(SeZhiTouXiangActivity.YONG_HU_TOU_XIANG)
        if (local.isBlank()) {
            local = "0"
        }

        if (local == "0") {
            binding.chouTiDrawerTouXiang.setImageResource(R.drawable.dres_119)
        } else if (local == "1") {
            binding.chouTiDrawerTouXiang.setImageResource(R.drawable.dres_120)
        } else if (local == "2") {
            binding.chouTiDrawerTouXiang.setImageResource(R.drawable.dres_121)
        } else if (local == "3") {
            binding.chouTiDrawerTouXiang.setImageResource(R.drawable.dres_122)
        } else if (local == "4") {
            binding.chouTiDrawerTouXiang.setImageResource(R.drawable.dres_123)
        } else if (local == "5") {
            binding.chouTiDrawerTouXiang.setImageResource(R.drawable.dres_124)
        }

        binding.chouTiDrawerTouXiang.setOnClickListener {
            onClickAction.invoke(10)
            parentActivity?.toNextActivity(
                activity = SeZhiTouXiangActivity::class.java,
                mode = IntentFlagMode.NEW_TASK
            )

            //
            addTouch(type = "chou_ti_tou_xiang_click")
        }

        binding.chouTiDrawerProfile.setOnClickListener {
            onClickAction.invoke(0)
            parentActivity?.toNextActivity(
                activity = RenZhengMyProfileActivity::class.java,
                mode = IntentFlagMode.NEW_TASK
            )

            //
            addTouch(type = "chou_ti_my_profile_click")
        }

        binding.chouTiDrawerFangKui.setOnClickListener {
            onClickAction.invoke(1)
            parentActivity?.toNextActivity(
                activity = FangKuiActivity::class.java,
                mode = IntentFlagMode.NEW_TASK
            )

            //
            addTouch(type = "chou_ti_fang_kui_click")
        }

        binding.chouTiDrawerXieYi.setOnClickListener {
            onClickAction.invoke(2)
            parentActivity?.toNextActivity(
                activity = XieYiActivity::class.java,
                mode = IntentFlagMode.NEW_TASK
            )

            //
            addTouch(type = "chou_ti_xie_yi_click")
        }

        binding.chouTiDrawerExit.setOnClickListener {
            onClickAction.invoke(3)
            parentActivity?.toNextActivity(
                activity = SeZhiActivity::class.java,
                mode = IntentFlagMode.NEW_TASK
            )

            //
            addTouch(type = "chou_ti_she_zhi_click")
        }
    }
}